package com.shermanzhou.learn.learnjava8.streamapi.exercise;

import com.shermanzhou.learn.learnjava8.streamapi.model.Trader;
import com.shermanzhou.learn.learnjava8.streamapi.model.Transaction;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * @author zhouchaoqun
 * @email zhouchaoqun@quancheng-ec.com
 * @created 2017/10/3 上午10:42
 */
public class TestTransaction {
    List<Transaction> transactions = null;

    @Before
    public void before() {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
    }

    // 1. 找出 2011 年发生的所有交易，并按交易额排序（从低到高）
    @Test
    public void test1() {
        transactions.stream()
                .filter((t) -> t.getYear() == 2011)
                .sorted(Comparator.comparingInt(Transaction::getValue))
                .forEach(System.out::println);
    }

    // 2. 交易员都在那些城市工作过
    @Test
    public void test2() {
        transactions.stream()
                .map(Transaction::getTrader)
                .map(Trader::getCity)
                .distinct()
                .forEach(System.out::println);
    }

    // 3. 查找所有来自剑桥的交易员，并按姓名排序
    @Test
    public void test3() {
        transactions.stream()
                .map(Transaction::getTrader)
                .filter((t) -> t.getCity().equals("Cambridge"))
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .forEach(System.out::println);
    }

    // 4. 返回所有交易员的姓名字符串，按字母顺序排序
    @Test
    public void test4() {
        transactions.stream()
                .map(Transaction::getTrader)
                .map(Trader::getName)
                .distinct()
                .sorted(String::compareTo)
                .forEach(System.out::println);
    }

    // 5. 有没有交易员是在米兰工作的?
    @Test
    public void test5() {

        boolean b = transactions.stream()
                .anyMatch((t) -> t.getTrader().getCity().equals("Milan"));
        System.out.println(b);
    }

    // 6. 打印所有生活在剑桥的交易员的所有交易额
    @Test
    public void test6() {
        Optional<Integer> camBridgeWholeValue = transactions.stream()
                .filter((t) -> t.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .reduce(Integer::sum);
        System.out.println(camBridgeWholeValue.get());
    }

    // 7. 所有交易中，最高的交易额是多少
    @Test
    public void test7() {
        long start = System.currentTimeMillis();

        Optional<Transaction> max = transactions.stream()
                .max(Comparator.comparingInt(Transaction::getValue));
        System.out.println(max.get().getValue());

        long end = System.currentTimeMillis();
        System.out.println(end - start);
        System.out.println("---------------------------");

        start = System.currentTimeMillis();
        Optional<Integer> max1 = transactions.stream()
                .map(Transaction::getValue)
                .max(Integer::compare);
        System.out.println(max1.get());
        end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    // 8. 找到交易额最小的交易
    @Test
    public void test8() {
        Optional<Transaction> min = transactions.stream()
                .min(Comparator.comparingInt(Transaction::getValue));
        System.out.println(min.get());
    }

    @Test
    public void test9() {
        Consumer consumer = System.out::print;
        Consumer consumer1 = consumer.andThen(System.out::print);
        consumer1.accept("aaa");
    }

}
